package com.jsy.mqhelper.charlotterpc.protocol;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.*;

/**
 * @author: SongyangJi
 * @description:
 * @since: 2021/9/25
 */

@ToString
@NoArgsConstructor
public class CharlotteRpcRequest {
    //    String jsonRpcVersion;
    //    String id;

    /**
     * 方法的唯一标识符（暂实现为 method.toString() ）
     */
    @Getter
    @Setter
    private String methodId;

    /**
     * 方法的参数
     */
    @JsonIgnore
    private Object[] params;
    /**
     * params 的包装
     */
    @Getter
    @Setter
    private ObjectWrapper[] paramsWrappers;


    public CharlotteRpcRequest(String methodId, Object[] params) {
        this.methodId = methodId;
        this.paramsWrappers = ObjectWrapper.wrap(params);
    }


    @JsonIgnore
    public Object[] getParams() {
        if (params == null) {
            unwrapObjectWrapper();
        }
        return params;
    }

    private void unwrapObjectWrapper() {
        if (paramsWrappers == null) {
            params = new Object[0];
            return;
        }
        int len = paramsWrappers.length;
        params = new Object[len];
        for (int i = 0; i < len; i++) {
            params[i] = paramsWrappers[i].unwrap();
        }
    }

}

